package com.jinlejia.erp.ui.house.prospect.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;

import com.jinlejia.erp.R;
import com.jinlejia.erp.ui.house.prospect.Helper;
import com.wx.wheelview.adapter.ArrayWheelAdapter;
import com.wx.wheelview.widget.WheelView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王进 on 2019/1/2.
 */

public class ProspectSelectHouseTypeDialog  extends AlertDialog {
    private Context context;

    //住宅室厅卫厨
    private ArrayList<Map<String,Object>> residenceS = new ArrayList<>();
    private ArrayList<Map<String,Object>> residenceT = new ArrayList<>();
    private ArrayList<Map<String,Object>> residenceW = new ArrayList<>();
    private ArrayList<Map<String,Object>> residenceC = new ArrayList<>();
    //公寓室厅卫厨
    private ArrayList<Map<String,Object>> apartmentS = new ArrayList<>();
    private ArrayList<Map<String,Object>> apartmentT = new ArrayList<>();
    private ArrayList<Map<String,Object>> apartmentW = new ArrayList<>();
    private ArrayList<Map<String,Object>> apartmentC = new ArrayList<>();
    //别墅室厅卫厨
    private ArrayList<Map<String,Object>> villaS = new ArrayList<>();
    private ArrayList<Map<String,Object>> villaT = new ArrayList<>();
    private ArrayList<Map<String,Object>> villaW = new ArrayList<>();
    private ArrayList<Map<String,Object>> villaC = new ArrayList<>();

    /**
     * 住宅类户型选项值：卧室1-6、厅1-2、卫生间0-4、厨房0-2
     */
    public static int RESIDENTIAL_TYPE = 0;

    /**
     * 公寓类户型选项值：卧室1-1、厅1-1、卫生间0-1、厨房0-1
     */
    public static int APARTMENT_TYPE = 1;
    /**
     * 别墅类户型选项值：卧室3-8、厅3-4、卫生间3-6、厨房1-2、别墅楼体正面全景1-1
     */
    public static int VILLA_TYPE = 2;


    public static String KEY_TOILET = "toilet";
    public static String KEY_KITCHEN = "kitchen";
    public static String KEY_DRAWING_ROOM = "drawingRoom";
    public static String KEY_ROOM = "room";
    private int houseUsage = 0;
    private Helper.Util.HouseInfo info;
    public ProspectSelectHouseTypeDialog(Context c, @IntRange(from = 0,to = 2) int type, Helper.Util.HouseInfo defaultInfo, ArrayList<Map<String, Object>> residenceS, ArrayList<Map<String, Object>> residenceT, ArrayList<Map<String, Object>> residenceW, ArrayList<Map<String, Object>> residenceC, ArrayList<Map<String, Object>> apartmentS, ArrayList<Map<String, Object>> apartmentT, ArrayList<Map<String, Object>> apartmentW, ArrayList<Map<String, Object>> apartmentC, ArrayList<Map<String, Object>> villaS, ArrayList<Map<String, Object>> villaT, ArrayList<Map<String, Object>> villaW, ArrayList<Map<String, Object>> villaC) {
        super(c);
        this.context = c;
        this.houseUsage = type;
        this.info = defaultInfo;
        this.residenceS = residenceS;
        this.residenceT = residenceT;
        this.residenceW = residenceW;
        this.residenceC = residenceC;
        this.apartmentS = apartmentS;
        this.apartmentT = apartmentT;
        this.apartmentW = apartmentW;
        this.apartmentC = apartmentC;
        this.villaS = villaS;
        this.villaT = villaT;
        this.villaW = villaW;
        this.villaC = villaC;

    }

    private String colorStr = "#F1BE0D";
    private int textMargin = 60;
    private int textSize = 38;
    private int wheelCount = 5;

    private final boolean loop = false;
    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        window.setContentView(R.layout.dialog_choose_house_type);
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawable(new ColorDrawable());

        if (houseUsage ==VILLA_TYPE){
            textMargin = 40;
            textSize = 38;
        }

        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.selectedTextColor = Color.parseColor(colorStr);
        style.textColor = Color.GRAY;
        style.selectedTextSize = 20;
        style.holoBorderColor = Color.GRAY;

        final WheelView wv1 = window.findViewById(R.id.wheelview1);
        wv1.setWheelAdapter(new ArrayWheelAdapter(context));
        List roomNum = getRoomNum();
        wv1.setWheelData(roomNum);
        wv1.setLoop(loop);
        wv1.setSkin(WheelView.Skin.Holo);
        wv1.setStyle(style);
        wv1.setWheelSize(wheelCount);
        wv1.setExtraText("室", Color.parseColor(colorStr), textSize, textMargin);
        if (info!=null){
            int count = info.getRoomCount();
            for (int i = 0; i < roomNum.size(); i++) {
                String s = String.valueOf(roomNum.get(i));
                int parseInt = Integer.parseInt(s);
                if (parseInt==count){
                    wv1.setSelection(i);
                }
            }
        }



        final WheelView wv2 = window.findViewById(R.id.wheelview2);
        wv2.setWheelAdapter(new ArrayWheelAdapter(context));
        wv2.setSkin(WheelView.Skin.Holo);
        List drawingNum = getDrawingNum();
        wv2.setWheelData(drawingNum);
        wv2.setLoop(loop);
        wv2.setWheelSize(wheelCount);
        wv2.setStyle(style);
        wv2.setExtraText("厅", Color.parseColor(colorStr), textSize, textMargin);

        if (info!=null){
            int count = info.getDrawingCount();
            for (int i = 0; i < drawingNum.size(); i++) {
                String s = String.valueOf(drawingNum.get(i));
                int parseInt = Integer.parseInt(s);
                if (parseInt==count){
                    wv2.setSelection(i);
                }
            }
        }

        final WheelView wv3 = window.findViewById(R.id.wheelview3);
        wv3.setWheelAdapter(new ArrayWheelAdapter(context));
        wv3.setSkin(WheelView.Skin.Holo);
        List kitchenNum = getKitchenNum();
        wv3.setWheelData(kitchenNum);
        wv3.setLoop(loop);
        wv3.setStyle(style);
        wv3.setWheelSize(wheelCount);
        wv3.setExtraText("厨", Color.parseColor(colorStr), textSize, textMargin);

        if (info!=null){
            int count = info.getKitchenCount();
            for (int i = 0; i < kitchenNum.size(); i++) {
                String s = String.valueOf(kitchenNum.get(i));
                int parseInt = Integer.parseInt(s);
                if (parseInt==count){
                    wv3.setSelection(i);
                }
            }
        }


        final WheelView wv4 = window.findViewById(R.id.wheelview4);
        wv4.setWheelAdapter(new ArrayWheelAdapter(context));
        wv4.setSkin(WheelView.Skin.Holo);
        List toiletNum = getToiletNum();
        wv4.setWheelData(getToiletNum());
        wv4.setLoop(loop);
        wv4.setStyle(style);
        wv4.setWheelSize(wheelCount);
        wv4.setExtraText("卫", Color.parseColor(colorStr), textSize, textMargin);

        if (info!=null){
            int count = info.getToiletCount();
            for (int i = 0; i < toiletNum.size(); i++) {
                String s = String.valueOf(toiletNum.get(i));
                int parseInt = Integer.parseInt(s);
                if (parseInt==count){
                    wv4.setSelection(i);
                }
            }
        }

        window.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        window.findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( null != listener ){
                    Map<String,Integer> map = new HashMap<>();
                    //房间
                    map.put(KEY_ROOM,Integer.parseInt((String)wv1.getSelectionItem()));
                    //drawingNum 客厅
                    map.put(KEY_DRAWING_ROOM,Integer.parseInt((String)wv2.getSelectionItem()));
                    //kitchen 厨房
                    map.put(KEY_KITCHEN,Integer.parseInt((String)wv3.getSelectionItem()));
                    //toilet 厕所
                    map.put(KEY_TOILET,Integer.parseInt((String)wv4.getSelectionItem()));
                    listener.selectResult(map);
                }
                dismiss();
            }
        });

    }

    private OnSelectListener listener;

    public void setListener(OnSelectListener listener) {
        this.listener = listener;
    }

    public interface OnSelectListener{
        void selectResult(Map<String, Integer> result);
    }


    @NonNull
    private List getRoomNum(){
        ArrayList<String> dataS = new ArrayList<>();
        if (houseUsage ==APARTMENT_TYPE){
            for (Map<String,Object> map : apartmentS) {
                Object valueName = map.get("valueName");
                dataS.add((String) valueName);
            }
        }else if (houseUsage == RESIDENTIAL_TYPE){
            for (Map<String,Object> map : residenceS) {
                Object valueName = map.get("valueName");
                dataS.add((String) valueName);
            }
        }else if (houseUsage == VILLA_TYPE){
            for (Map<String,Object> map : villaS) {
                Object valueName = map.get("valueName");
                dataS.add((String) valueName);
            }
        }else {
            throw new IllegalArgumentException();
        }
        return dataS;
    }


    @NonNull
    private List getDrawingNum(){
        ArrayList<String> dataS = new ArrayList<>();
        if (houseUsage ==APARTMENT_TYPE){
            for (Map<String,Object> map : apartmentT) {
                Object valueName = map.get("valueName");
                dataS.add((String) valueName);
            }
        }else if (houseUsage == RESIDENTIAL_TYPE){
            for (Map<String,Object> map : residenceT) {
                Object valueName = map.get("valueName");
                dataS.add((String) valueName);
            }
        }else if (houseUsage == VILLA_TYPE){
            for (Map<String,Object> map : villaT) {
                Object valueName = map.get("valueName");
                dataS.add((String) valueName);
            }
        }else {
            throw new IllegalArgumentException();
        }
        return dataS;
    }

    @NonNull
    private List getToiletNum(){
        ArrayList<String> dataS = new ArrayList<>();
        if (houseUsage ==APARTMENT_TYPE){
            for (Map<String,Object> map : apartmentW) {
                Object valueName = map.get("valueName");
                dataS.add((String) valueName);
            }
        }else if (houseUsage == RESIDENTIAL_TYPE){
            for (Map<String,Object> map : residenceW) {
                Object valueName = map.get("valueName");
                dataS.add((String) valueName);
            }
        }else if (houseUsage == VILLA_TYPE){
            for (Map<String,Object> map : villaW) {
                Object valueName = map.get("valueName");
                dataS.add((String) valueName);
            }
        }else {
            throw new IllegalArgumentException();
        }
        return dataS;
    }
    @NonNull
    private List getKitchenNum(){
        ArrayList<String> dataS = new ArrayList<>();
        if (houseUsage ==APARTMENT_TYPE){
            for (Map<String,Object> map : apartmentC) {
                Object valueName = map.get("valueName");
                dataS.add((String) valueName);
            }
        }else if (houseUsage == RESIDENTIAL_TYPE){
            for (Map<String,Object> map : residenceC) {
                Object valueName = map.get("valueName");
                dataS.add((String) valueName);
            }
        }else if (houseUsage == VILLA_TYPE){
            for (Map<String,Object> map : villaC) {
                Object valueName = map.get("valueName");
                dataS.add((String) valueName);
            }
        }else {
            throw new IllegalArgumentException();
        }
        return dataS;
    }


}
